package application.mvc.biz.authorize;

import java.net.URL;
import java.util.ResourceBundle;
import application.common.plugins.serialPort.SerialCommunication;
import application.common.ui.ProgressStage;
import application.common.util.LayerUtil;
import application.common.util.RouteUtil;
import application.common.util.ShiroUtil;
import javafx.animation.Animation;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.RotateTransition;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.util.Duration;

public class AuthorizeController implements Initializable {

	private StringProperty storeId = new SimpleStringProperty();

	@FXML
	private Label headerLabel;

	@FXML
	private Label copyrightLabel;

	@FXML
	private Label messageLabel;

	@FXML
	private Button openBoxBtn;

	@FXML
	private ImageView portraitImageView;

	@FXML
	private Label usernameLabel;

	@FXML
	private Label roleNameLabel;

	@FXML
	private Label cabinetHeaderLabel;

	@FXML
	private FlowPane buttonsFlowPane;

	@FXML
	private Pane timerOutLinePane;

	@FXML
	private Pane timerInLinePane;

	@FXML
	private Label timerLabel;
	private Timeline animation;
	private RotateTransition rotateTransitionOut;
	private RotateTransition rotateTransitionIn;

	private SerialCommunication serialCommunication;

	private ObservableList<Button> dataList = FXCollections.observableArrayList();

	public AuthorizeController(String storeId) {
		this.storeId.set(storeId);
	}

	public void initialize(URL arg0, ResourceBundle arg1) {
		headerLabel.setText(ShiroUtil.getSoftName());
		copyrightLabel.setText(ShiroUtil.getCopyright());
		portraitImageView.setImage(ShiroUtil.getUser().getPortrait());
		usernameLabel.setText(ShiroUtil.getUser().getName());
		roleNameLabel.setText(ShiroUtil.getUser().getRoleName());
		ProgressStage.of(RouteUtil.mainStage, new Task<Void>() {
			@Override
			protected Void call() throws Exception {
				Platform.runLater(new Runnable() {
					public void run() {
							buttonsFlowPane.getChildren().addAll(dataList);
							rotateTransitionOut = new RotateTransition(Duration.seconds(10), timerOutLinePane);
							rotateTransitionOut.setFromAngle(0);
							rotateTransitionOut.setToAngle(360);
							rotateTransitionOut.setByAngle(360); // 设置旋转的角度
							rotateTransitionOut.setCycleCount(Animation.INDEFINITE); // 设置旋转次数，我们需要旋转无数次
							rotateTransitionOut.setInterpolator(Interpolator.LINEAR);
							rotateTransitionOut.play();

							rotateTransitionIn = new RotateTransition(Duration.seconds(10), timerInLinePane);
							rotateTransitionIn.setFromAngle(360);
							rotateTransitionIn.setToAngle(0);
							rotateTransitionIn.setByAngle(360); // 设置旋转的角度
							rotateTransitionIn.setCycleCount(Animation.INDEFINITE); // 设置旋转次数，我们需要旋转无数次
							rotateTransitionIn.setInterpolator(Interpolator.LINEAR);
							rotateTransitionIn.play();
							startCountDown();
					}
				});
				return null;
			}
		}, "初始化卷宗柜中...").show();

	}

	public void exit(ActionEvent event) {
		
	}

	public void next(ActionEvent event) {
		
	}

	public void startCountDown() {
		animation = new Timeline(new KeyFrame(Duration.millis(1000), new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				if(RouteUtil.getTime()>0) {
					RouteUtil.setTime(RouteUtil.getTime() - 1);
					if (RouteUtil.getTime() == 0) {
						timerLabel.setText("0");
						LayerUtil.open("操作超时！", 999);
					} else if (RouteUtil.getTime() > 0) {
						timerLabel.setText(RouteUtil.getTime() + "");
					}
				}
				
			}
		}));
		animation.setCycleCount(Timeline.INDEFINITE);
		animation.play();
	}

	public void destroy() {
		if (animation != null) {
			animation.stop();
		}
		if (rotateTransitionOut != null) {
			rotateTransitionOut.stop();
		}
		if (rotateTransitionIn != null) {
			rotateTransitionIn.stop();
		}
		if (serialCommunication != null) {
			serialCommunication.release();
		}
	}

	public void message(final String message, final boolean isOK) {
		Platform.runLater(new Runnable() {
			public void run() {
				messageLabel.setText(message);
				if (isOK) {
					messageLabel.setId("message-label");
				} else {
					messageLabel.setId("message-label-error");
				}
			}
		});

	}
}

class UserAuthPane{
	
}
